{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "文件夹 '0217' 已存在。\n",
      "文件夹 '梅川镇' 已存在。\n",
      "已成功删除字段: MOID, WTBH, WTMX, SQMC, JDMC, GID, FKSJ, EXISTSJZW, ObjectID\n",
      "没有需要删除的字段\n",
      "合并完成\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "from datetime import datetime\n",
    "import arcpy\n",
    "from pypinyin import lazy_pinyin\n",
    "\n",
    "def check_and_create_folder(folder_path, folder_name):\n",
    "    full_path = os.path.join(folder_path, folder_name)\n",
    "\n",
    "    if not os.path.exists(full_path):\n",
    "        os.makedirs(full_path)\n",
    "        print(f\"文件夹 '{folder_name}' 已创建。\")\n",
    "    else:\n",
    "        print(f\"文件夹 '{folder_name}' 已存在。\")\n",
    "\n",
    "    return full_path\n",
    "\n",
    "def create_town(base_path, townName):\n",
    "    # 检查并创建镇文件夹\n",
    "    town_folder = check_and_create_folder(base_path, townName)\n",
    "\n",
    "    return town_folder\n",
    "\n",
    "isagain = False\n",
    "version = \"02\"\n",
    "path_to_check = r\"E:\\MyDocument\\ArcGis\\DT\\基地面修改\"\n",
    "folder_to_find = datetime.now().strftime(\"%m%d\")\n",
    "\n",
    "base_path = check_and_create_folder(path_to_check, folder_to_find)\n",
    "\n",
    "townName = \"梅川镇\"\n",
    "# 查找对应名称的图层\n",
    "modify_layer_name = \"梅川镇_不修改\"\n",
    "nomodify_layer_name = \"梅川镇_要修改\"\n",
    "\n",
    "town_folder = create_town(base_path, townName)\n",
    "merge_name = townName + \"_中心镇区\" + (version if isagain else '')\n",
    "merge_path = os.path.join(town_folder, merge_name)\n",
    "\n",
    "# 获取当前工程\n",
    "aprx = arcpy.mp.ArcGISProject(\"CURRENT\")\n",
    "\n",
    "# 获取活动的地图或场景\n",
    "active_map_or_scene = aprx.activeMap\n",
    "\n",
    "\n",
    "modify_layer = None\n",
    "nomodify_layer = None\n",
    "for lyr in active_map_or_scene.listLayers():\n",
    "    if lyr.name == modify_layer_name:\n",
    "        modify_layer = lyr\n",
    "    elif lyr.name == nomodify_layer_name:\n",
    "        nomodify_layer = lyr\n",
    "\n",
    "if modify_layer is None:\n",
    "    print(\"modify_layer图层未找到。\")\n",
    "    exit\n",
    "\n",
    "if nomodify_layer is None:\n",
    "    print(\"nomodify_layer图层未找到。\")\n",
    "    exit\n",
    "    \n",
    "# 字段删除操作\n",
    "if modify_layer:\n",
    "    fields_to_keep = [\"FID\", \"Shape\", \"建筑顶\", \"建筑底\", \"建筑层\", \"uuid\", \"结构\", \"建筑高\", \"面积\", \"x\", \"y\", \"sqjcwhmc\"]\n",
    "\n",
    "    # 获取所有字段，并过滤出不需要保留的字段\n",
    "    all_fields = [field.name for field in arcpy.ListFields(modify_layer)]\n",
    "    fields_to_delete = [field for field in all_fields if field not in fields_to_keep]\n",
    "\n",
    "    if fields_to_delete:\n",
    "        try:\n",
    "            # 删除不需要保留的字段\n",
    "            arcpy.management.DeleteField(modify_layer, fields_to_delete)\n",
    "            print(f\"已成功删除字段: {', '.join(fields_to_delete)}\")\n",
    "        except arcpy.ExecuteError as e:\n",
    "            print(f\"发生错误: {e}\")\n",
    "    else:\n",
    "        print(\"没有需要删除的字段\")\n",
    "else:\n",
    "    print(\"图层未找到。\")\n",
    "\n",
    "if nomodify_layer:\n",
    "    fields_to_keep = [\"FID\", \"Shape\", \"建筑顶\", \"建筑底\", \"建筑层\", \"uuid\", \"结构\", \"建筑高\", \"面积\", \"x\", \"y\", \"sqjcwhmc\"]\n",
    "\n",
    "    # 获取所有字段，并过滤出不需要保留的字段\n",
    "    all_fields = [field.name for field in arcpy.ListFields(nomodify_layer)]\n",
    "    fields_to_delete = [field for field in all_fields if field not in fields_to_keep]\n",
    "\n",
    "    if fields_to_delete:\n",
    "        try:\n",
    "            # 删除不需要保留的字段\n",
    "            arcpy.management.DeleteField(nomodify_layer, fields_to_delete)\n",
    "            print(f\"已成功删除字段: {', '.join(fields_to_delete)}\")\n",
    "        except arcpy.ExecuteError as e:\n",
    "            print(f\"发生错误: {e}\")\n",
    "    else:\n",
    "        print(\"没有需要删除的字段\")\n",
    "else:\n",
    "    print(\"图层未找到。\")\n",
    "\n",
    "# 执行合并操作\n",
    "inputs = [modify_layer_name, nomodify_layer_name]\n",
    "arcpy.management.Merge(\n",
    "    inputs,\n",
    "    merge_path,\n",
    "    \"\",\n",
    "    \"NO_SOURCE_INFO\"\n",
    ")\n",
    "print(\"合并完成\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ArcGISPro",
   "language": "Python",
   "name": "python3"
  },
  "language_info": {
   "file_extension": ".py",
   "name": "python",
   "version": "3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
